package com.tom;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

/**
 * @author tdl
 * @since 2024-03-21
 */
@RestController
@RequestMapping("building")
public class BuilderController {

    @Resource
    private Configuration configuration;

    @PostMapping("form")
    public String buildingForm(@RequestBody BuilderDTO params) throws IOException, TemplateException {
        Template template = configuration.getTemplate("form-template.ftl");
        template.process(params, new FileWriter(params.getName() + ".vue"));
        return "ok";
    }

    @PostMapping("list")
    public String buildingList(@RequestBody ListBuilderDTO params) throws IOException, TemplateException {
        Template template = configuration.getTemplate("list-template.ftl");
        template.process(params, new FileWriter(params.getName() + ".vue"));

        Template template2 = configuration.getTemplate("modal-data-template.ftl");
        template2.process(params, new FileWriter("modal.data.js"));
        return "ok";
    }

    @PostMapping("detail")
    public String buildingDetail(@RequestBody BuilderDTO params) throws IOException, TemplateException {
        Template template = configuration.getTemplate("detail-template.ftl");
        template.process(params, new FileWriter(params.getName() + ".vue"));
        return "ok";
    }
}
